﻿/* SystemCommandsHelper.cs
 *
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SystemCommands
{
    [Flags]
    public enum ExitWindows : uint
    {
        // ONE of the following five:
        LogOff = 0x00,
        ShutDown = 0x01,
        Reboot = 0x02,
        PowerOff = 0x08,
        RestartApps = 0x40,
        // plus AT MOST ONE of the following two:
        Force = 0x04,
        ForceIfHung = 0x10,
    }

    [Flags]
    enum ShutdownReason : uint
    {
        MajorApplication = 0x00040000,
        MajorHardware = 0x00010000,
        MajorLegacyApi = 0x00070000,
        MajorOperatingSystem = 0x00020000,
        MajorOther = 0x00000000,
        MajorPower = 0x00060000,
        MajorSoftware = 0x00030000,
        MajorSystem = 0x00050000,

        MinorBlueScreen = 0x0000000F,
        MinorCordUnplugged = 0x0000000b,
        MinorDisk = 0x00000007,
        MinorEnvironment = 0x0000000c,
        MinorHardwareDriver = 0x0000000d,
        MinorHotfix = 0x00000011,
        MinorHung = 0x00000005,
        MinorInstallation = 0x00000002,
        MinorMaintenance = 0x00000001,
        MinorMMC = 0x00000019,
        MinorNetworkConnectivity = 0x00000014,
        MinorNetworkCard = 0x00000009,
        MinorOther = 0x00000000,
        MinorOtherDriver = 0x0000000e,
        MinorPowerSupply = 0x0000000a,
        MinorProcessor = 0x00000008,
        MinorReconfig = 0x00000004,
        MinorSecurity = 0x00000013,
        MinorSecurityFix = 0x00000012,
        MinorSecurityFixUninstall = 0x00000018,
        MinorServicePack = 0x00000010,
        MinorServicePackUninstall = 0x00000016,
        MinorTermSrv = 0x00000020,
        MinorUnstable = 0x00000006,
        MinorUpgrade = 0x00000003,
        MinorWMI = 0x00000015,

        FlagUserDefined = 0x40000000,
        FlagPlanned = 0x80000000
    }

    public class SystemCommandsHelper
    {
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool LockWorkStation();

        /// <summary>
        /// Logs of the current user
        /// </summary>
        /// <returns></returns>
        public static bool LogOff()
        {
            return SystemCommandsHelper.ExitWindowsEx((uint)ExitWindows.LogOff, (uint)(ShutdownReason.MajorOther & ShutdownReason.MinorOther));
        }

        /// <summary>
        /// Shut down the computer 
        /// </summary>
        /// <remarks>
        /// Based on kb 317371 
        /// "How To Use the Remote Shutdown Tool to Shut Down and Restart a Computer in Windows 2000"
        /// http://support.microsoft.com/kb/317371
        /// </remarks>
        internal static void Shutdown()
        {
            System.Diagnostics.Process.Start("shutdown", "/l");
        }

        /// <summary>
        /// Forces to shut down the computer 
        /// </summary>
        /// <remarks>
        /// Based on kb 317371 
        /// "How To Use the Remote Shutdown Tool to Shut Down and Restart a Computer in Windows 2000"
        /// http://support.microsoft.com/kb/317371
        /// </remarks>
        internal static void ForceShutdown()
        {
            System.Diagnostics.Process.Start("shutdown", "/l /y /c");
        }

        /// <summary>
        /// Restart the computer 
        /// </summary>
        /// <remarks>
        /// Based on kb 317371 
        /// "How To Use the Remote Shutdown Tool to Shut Down and Restart a Computer in Windows 2000"
        /// http://support.microsoft.com/kb/317371
        /// </remarks>
        internal static void Restart()
        {
            System.Diagnostics.Process.Start("shutdown", "/r");
        }

        /// <summary>
        /// Force restart the computer 
        /// </summary>
        /// <remarks>
        /// Based on kb 317371 
        /// "How To Use the Remote Shutdown Tool to Shut Down and Restart a Computer in Windows 2000"
        /// http://support.microsoft.com/kb/317371
        /// </remarks>
        internal static void ForceRestart()
        {
            System.Diagnostics.Process.Start("shutdown", "/r /y /c");
        }

        internal static void Sleep()
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Suspend, true, false);
        }

        internal static void LockComputer()
        {
            bool result = LockWorkStation();

            if (result == false)
            {
                // An error occured
                // throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
    }
}
